package com.bjfu.servicebase.exceptionhandler;


import com.bjfu.common_utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody // 为了返回json数据
    public R error(Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return R.error().message(e.getMessage().split(":")[0]);
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R error(NullPointerException e){
        e.printStackTrace();
        return R.error().message("执行了NullPointerException异常处理");
    }

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public R error(SQLException e){
        e.printStackTrace();
        return R.error().message("执行了SQLException处理");
    }

    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public R error(GuliException e){
        log. error(e.getMsg());
        e.printStackTrace();
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}
